using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input.Touch;


namespace Pokemon
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class ScreenManager : DrawableGameComponent
    {
        #region Attribute

        private Texture2D _background;

        private readonly List<GameScreen> _screens;

        private GameScreen _activeScreen;

        private SpriteBatch _spriteBatch;

        private SpriteFont _spriteFont;

#endregion

        /// <summary>
        /// Contructor of this class
        /// </summary>
        /// <param name="game"></param>
        public ScreenManager(Game game)
            : base(game)
        {
            _screens = new List<GameScreen>();
            _activeScreen = null;
        }
        
        #region Properties

        public SpriteBatch SpriteBatch
        {
            get { return _spriteBatch; }
            set { _spriteBatch = value; }
        }

        public SpriteFont SpriteFont
        {
            get { return _spriteFont; }
            set { _spriteFont = value; }
        }

#endregion

        /// <summary>
        /// Load content
        /// </summary>
        protected override void LoadContent()
        {          
            _background = Game.Content.Load<Texture2D>(@"Textures\Backgrounds\blank");
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            Game.Services.AddService(typeof(SpriteBatch), _spriteBatch);
            _spriteFont = Game.Content.Load<SpriteFont>(@"Fonts\PokemonMenuFont");
            foreach (var gameScreen in _screens)
            {
                gameScreen.LoadContent();
            }
            base.LoadContent();
        }

        /// <summary>
        /// Unload content
        /// </summary>
        protected override void UnloadContent()
        {           
            _background.Dispose();
            foreach (var gameScreen in _screens)
            {
                gameScreen.UnloadContent();
            }
            base.UnloadContent();
        }


        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {    
            //Only update the ActiveScreen        
            _activeScreen.Update(gameTime);             
        }

        /// <summary>
        /// Draw on the screen
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            //Only Draw the ActiveScreen
            _activeScreen.Draw(gameTime);            
        }

        /// <summary>
        /// Add a new screen
        /// </summary>
        /// <param name="gameScreen"></param>
        public void AddScreen(GameScreen gameScreen)
        {
            gameScreen.ScreenManager = this;
            _screens.Add(gameScreen);            
        }
        /// <summary>
        /// Remove a screen
        /// </summary>
        /// <param name="gameScreen"></param>
        public void RemoveScreen(GameScreen gameScreen)
        {
            _screens.Remove(gameScreen);
            return;
        }

        public void SwitchScreen(GameScreen gameScreen)
        {
            _activeScreen = gameScreen;
        }

        public void Play()
        {
            foreach (var gameScreen in _screens)
            {
                if (gameScreen is GameplayScreen)
                {
                    SwitchScreen(gameScreen);
                    return;
                }
            }
        }
    }
}
